//
//  MenuBookChoose.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-8.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "MenuBookChoose.h"
#import "PopoverLanguageSelect.h"
#import "PopoverTableViewController.h"
#import "CookSelectView.h"
#import "MessageBoxBaseView.h"
#import "Imports1.h"
#import "RemoteMenuDetail.h"
#import "RemoteMenuDetail1.h"
#import "GridViewItemMenuBook.h"
#import "MAlertView.h"
#import "About.h"

#import "PwdModifyAlertView.h"
#import "MAlertView.h"

@implementation MenuBookChoose
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)buttonClicked:(id)sender {
    if (AppSession.isAnimating) {
        MyNSLog(@"上一个动画还未完成...");
        return;
    }
    [AppSession playMusic:VoiceFile(@"clicked.caf")];
    NSInteger bid = [sender tag];
    UIButton *btn = (UIButton *)[self.view viewWithTag:bid];
    if (bid == TagOfUIButton) {
        MyNSLog(@"远程餐单查询");         
        PopoverTableViewController *ptvc = [[PopoverTableViewController alloc] initWithNibName:@"PopoverTableViewController" bundle:nil popoverType:1];
        [ptvc PopOverFromRect:btn.frame inView:self.view directions:UIPopoverArrowDirectionDown];
        [ptvc release];        
    }else if(bid == TagOfUIButton + 1){
        MyNSLog(@"本地餐单查询");
        PopoverTableViewController *ptvc = [[PopoverTableViewController alloc] initWithNibName:@"PopoverTableViewController" bundle:nil popoverType:2];
        [ptvc PopOverFromRect:btn.frame inView:self.view directions:UIPopoverArrowDirectionDown];
        [ptvc release];  
    }else if(bid == TagOfUIButton + 2){
        MyNSLog(@"语言");
        PopoverLanguageSelect *pls = [[PopoverLanguageSelect alloc] initWithNibName:@"PopoverLanguageSelect" bundle:nil];
        [pls PopOverFromRect:btn.frame inView:self.view directions:UIPopoverArrowDirectionDown];
        [pls release];        
    }else if(bid == TagOfUIButton + 3){
        MyNSLog(@"酒店介绍");
        [AppSession initOtherImages:@"HotelInfo"];
        if (AppSession.otherImages.count > 0) {
            About *hotelInfo = [[About alloc] initWithNibName:@"About" withObj:@"hotelInfo"];
            MessageBoxBaseView *mbbv = [[MessageBoxBaseView alloc] initWithContentViewController:hotelInfo];
            [self.view addSubview:mbbv];
            [mbbv release];
        }else {
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                            message:TranslateStr(@"没有酒店介绍图片")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"返回") 
                                                  otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
    }else if(bid == TagOfUIButton + 4){
        MyNSLog(@"关于");           
        [AppSession initOtherImages:@"About"];
        if (AppSession.otherImages.count > 0) {
            About *about = [[About alloc] initWithNibName:@"About" withObj:@"about"];
            MessageBoxBaseView *mbbv = [[MessageBoxBaseView alloc] initWithContentViewController:about];
            [self.view addSubview:mbbv];
            [mbbv release];
        }else {
            UIAlertView *alert =[[ UIAlertView alloc] initWithTitle:TranslateStr(@"提示") 
                                                            message:TranslateStr(@"没有关于图片")
                                                           delegate:self 
                                                  cancelButtonTitle:TranslateStr(@"返回") 
                                                  otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
    }
}
 
-(void) PopOutRemoteMenuDetail{//弹出远程主单明细
    //根据menu号查询明细
    RemoteMenuDetail *rmd = [[RemoteMenuDetail alloc] initWithNibName:@"RemoteMenuDetail" bundle:nil];
    MessageBoxBaseView *mbbv = [[MessageBoxBaseView alloc] initWithContentViewController:rmd];
    [self.view addSubview:mbbv];
    [mbbv release];
    
//    RemoteMenuDetail *rmd = [[RemoteMenuDetail alloc] initWithNibName:@"RemoteMenuDetail" bundle:nil];
//    [self.view addSubview:rmd.view];
//    [rmd release];
}

#pragma mark - UIGridViewDataSource
-(NSInteger) gridViewRows:(UIGridView *)gridView{
    return 1;
}
-(NSInteger) gridViewCols:(UIGridView *)gridView{
    if (0<AppSession.pluBooks.count && AppSession.pluBooks.count < 3) {
        return AppSession.pluBooks.count;
    }else {
        return 3;
    }
}
-(NSInteger) gridViewTotalCount:(UIGridView *)gridView{
    return [AppSession.pluBooks count];
}
-(GridViewItem *) gridView:(UIGridView *)gridView1 itemAtIndex:(NSInteger)index{
    NSString *CellIdentifier = @"CellIdentifier1";
    GridViewItemMenuBook *item = (GridViewItemMenuBook *)[gridView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    if (item == nil){
        item = [[GridViewItemMenuBook alloc] init];
    }
    
//    GridViewItemMenuBook *item = [[GridViewItemMenuBook alloc] init];
    [item setData:[AppSession.pluBooks objectAtIndex:index]];
    return item;
}
-(BOOL) gridViewPageControlEnable:(UIGridView *)gridView{  //是否使用pageControl控件
    return NO;
}
-(BOOL) gridViewDirection:(UIGridView *)gridView{          //滑动的方向(YES-横向默认  NO-纵向)
    return YES;
}
-(BOOL) gridViewShowScrollIndicator:(UIGridView *)gridView{//是否显示滑动条Indicator(默认NO)
    return NO;
}
#pragma mark - UIGridViewDelegate
-(void) gridView:(UIGridView *)gridView didSelectItemAtIndex:(NSInteger)index{
    MyNSLog(@"点击了菜本 index=%d", index);
    //根据选中的菜本编号初始化所有菜谱数据
    if (AppSession.searchPlus != nil && [AppSession.searchPlus count]>0) {
        [AppSession.searchPlus removeAllObjects];//移除所有查询的临时菜品数组
    }
    AppSession.isCommendView = YES;
    [AppSession initPluMenuByBookIndex:index];//初始化大类数据
    [AppSession initCurrentOrderMenu:index];//初始化当前主单对象
    MyNSLog(@"menuCount=%d,sortCount=%d,commendedPluCount=%d",AppSession.pluMenus.count,AppSession.pluSorts.count,AppSession.commendPluPlus.count);    
    NSDictionary *infos = [NSDictionary dictionaryWithObjectsAndKeys:@"MenuBookChoose", @"FromNibName", @"GuestOrderingView",@"ToNibName",nil];
    postNWithInfos(SysMsg_RootView_ControllerChange, self, infos);
}

-(void) refreshUI{
    [((UIButton *)[self.view viewWithTag:TagOfUIButton]) setTitle:TranslateStr(@"已发送") forState:UIControlStateNormal];
    [((UIButton *)[self.view viewWithTag:TagOfUIButton+1]) setTitle:TranslateStr(@"未发送") forState:UIControlStateNormal];
    [((UIButton *)[self.view viewWithTag:TagOfUIButton+2]) setTitle:TranslateStr(@"语言") forState:UIControlStateNormal];
    [((UIButton *)[self.view viewWithTag:TagOfUIButton+3]) setTitle:TranslateStr(@"酒店介绍") forState:UIControlStateNormal];
    [((UIButton *)[self.view viewWithTag:TagOfUIButton+4]) setTitle:TranslateStr(@"关于") forState:UIControlStateNormal];
    [gridView reloadData];
}

#pragma mark - View lifecycle
//-(void)viewWillAppear:(BOOL)animated{MyNSLog(@"MenuBookChoose viewWillAppear");}
//-(void)viewWillDisappear:(BOOL)animated{MyNSLog(@"MenuBookChoose viewWillDisappear");}
//-(void)viewWillLayoutSubviews{MyNSLog(@"MenuBookChoose viewWillLayoutSubviews");}
//-(void)viewWillUnload{MyNSLog(@"MenuBookChoose viewWillUnload");}
- (void)viewDidLoad {  
    ////////////////////初始化数据 begin////////////////////
    [AppSession initMyOrderMenus:[NSString stringWithFormat:@"select * from MyOrderMenus where momType='1' order by momTime asc"]];//初始化已经保存但未发送的主单数组 
    [AppSession initRemoteMenus];
    ////////////////////初始化数据 end//////////////////////
    
//    addNObserver(@selector(PopOutRemoteMenuDetail), @"SysMsg_MenuBookChoose_PopOutRemoteMenuDetail");
    addNObserver(@selector(refreshUI), @"SysMsg_MenuBookChoose_refreshUI");
    postN(@"SysMsg_MenuBookChoose_refreshUI");
    //用自定义的九宫格view替换占位用的普通view，同时移除普通view
    UIView *tempView = (UIView *)[self.view viewWithTag:TagOfUIView];
    CGRect frame = tempView.frame;
    CGFloat width = frame.size.width;
    [tempView removeFromSuperview];
    
    if (AppSession.pluBooks.count==1) {
        frame = CGRectMake((1024-width/3)/2, frame.origin.y, width/3, frame.size.height);
    }else if (AppSession.pluBooks.count==2){
        frame = CGRectMake((1024-(width*2)/3)/2, frame.origin.y, (width*2)/3, frame.size.height);
    }
    
    gridView = [[UIGridView alloc] initWithTarget:self withFrame:frame withTag:TagOfUIGridView];
    [gridView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:gridView];
    [super viewDidLoad];
}

- (void)viewDidUnload{
    [super viewDidUnload];
}
-(void)dealloc{
    MyNSLog(@"MenuBookChoose is deallocing");
//    removeNObserver(@"SysMsg_MenuBookChoose_PopOutRemoteMenuDetail");
    removeNObserver(@"SysMsg_MenuBookChoose_refreshUI");
    [gridView release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
@end
